package com.bw.aop;


import com.bw.Mycommon.Mycommon;
import com.bw.domain.Operlog;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;

@Component
@Aspect
public class MyAop {


    @Autowired
    OperlogService operlogService;

    @Autowired
    HttpServletRequest request;

    @Around("@annotation(com.bw.Mycommon.Mycommon)")
    public Object getobject(ProceedingJoinPoint point) throws Throwable {

        Operlog operlog = new Operlog();

        Date date = new Date();

        //操作时间
        operlog.setOperTime(date);

        //错误信息

        //操作状态
        operlog.setStatus(0);

        //请求参数
        Object[] args = point.getArgs();
        String s = Arrays.toString(args);
        operlog.setOperParam(s);

        //操作地点
        operlog.setOperIp(request.getRemoteAddr());
        // 请求url
        operlog.setOperUrl(request.getRequestURL().toString());
        //部门名称
        operlog.setDeptName(request.getParameter("deptName"));
        //操作人员
        operlog.setOperName(request.getParameter("username"));
        //操作类别

        //请求方式
        operlog.setRequestMethod(request.getMethod());



        //方法名称
        String name = point.getSignature().getName();
        operlog.setMethod(name);




        MethodSignature signature = (MethodSignature)point.getSignature();
        Mycommon mycommon = signature.getMethod().getAnnotation(Mycommon.class);

        //业务类型
        operlog.setBusinessType(mycommon.type());

        //模块标题
        operlog.setTitle(mycommon.value());


//        operlog.setErrorMsg(point.proceed().toString());


        operlogService.save(operlog);



        return point.proceed();


    }
}
